/**
  ******************************************************************************
  * @file           : utils.c
  * @author         : lengq
  * @brief          : None
  * @attention      : None
  * @date           : 2024/4/9
  ******************************************************************************
  */


#include <math.h>
#include <stdbool.h>
#include "utils.h"
#include "MyLpUart.h"
//#include "rtdbg.h"


unsigned char GetMedianNum(int *bArray, int iFilterLen) {
    int i, j;// 循环变量
    unsigned char bTemp;

    // 用冒泡法对数组进行排序
    for (j = 0; j < iFilterLen - 1; j++) {
        for (i = 0; i < iFilterLen - j - 1; i++) {
            if (bArray[i] > bArray[i + 1]) {
                // 互换
                bTemp = bArray[i];
                bArray[i] = bArray[i + 1];
                bArray[i + 1] = bTemp;
            }
        }
    }

    // 计算中值
    if ((iFilterLen & 1) > 0) {
        // 数组有奇数个元素，返回中间一个元素
        bTemp = bArray[(iFilterLen + 1) / 2];
    } else {
        // 数组有偶数个元素，返回中间两个元素平均值
        bTemp = (bArray[iFilterLen / 2] + bArray[iFilterLen / 2 + 1]) / 2;
    }

    return bTemp;
}


/*中值滤波函数
//		Data:				滤波数据
//		Cnt:				滤波数据长度
//		FilterCnt:	滤除的数据长度，必须为2的倍数*/
int IntFilter(int16_t *Data, uint8_t Cnt, uint8_t FilterCnt) {
    int32_t sum = 0;
    int16_t temp;

    if (Cnt < 2)
        return *Data;

    for (int j = 0; j < Cnt - 1; j++) {
        for (int i = 0; i < Cnt - j - 1; i++) {
            if (Data[i] > Data[i + 1]) {
                temp = Data[i];
                Data[i] = Data[i + 1];
                Data[i + 1] = temp;
            }
        }
    }

    for (int count = FilterCnt / 2; count < Cnt - FilterCnt / 2; count++)
        sum += Data[count];
    return (sum / (Cnt - FilterCnt));
}

/*中值滤波函数
//		Data:				滤波数据
//		Cnt:				滤波数据长度
//		FilterCnt:	滤除的数据长度，必须为2的倍数*/
int IntFilter_32t(int32_t *Data, uint8_t Cnt, uint8_t FilterCnt) {
    int32_t sum = 0;
    int16_t temp;

    if (Cnt < 2)
        return *Data;

    for (int j = 0; j < Cnt - 1; j++) {
        for (int i = 0; i < Cnt - j - 1; i++) {
            if (Data[i] > Data[i + 1]) {
                temp = Data[i];
                Data[i] = Data[i + 1];
                Data[i + 1] = temp;
            }
        }
    }

    for (int count = FilterCnt / 2; count < Cnt - FilterCnt / 2; count++)
        sum += Data[count];
    return (sum / (Cnt - FilterCnt));
}

uint32_t IntFilter_u32t(uint32_t *Data, uint8_t Cnt, uint8_t FilterCnt) {
    int32_t sum = 0;
    int16_t temp;

    if (Cnt < 2)
        return *Data;

    for (int j = 0; j < Cnt - 1; j++) {
        for (int i = 0; i < Cnt - j - 1; i++) {
            if (Data[i] > Data[i + 1]) {
                temp = Data[i];
                Data[i] = Data[i + 1];
                Data[i + 1] = temp;
            }
        }
    }

    for (int count = FilterCnt / 2; count < Cnt - FilterCnt / 2; count++)
        sum += Data[count];
    return (sum / (Cnt - FilterCnt));
}

int32_t avg(const int32_t *Data, uint8_t Cnt) {
    int32_t sum = 0;
    for (int count = 0; count < Cnt; count++)
        sum += Data[count];
    return (sum / Cnt);
}


//题目描述：手动实现pow(x,n)的功能

//思路：考虑指数为正和为负和为0的情况，考虑溢出，考虑double与0的比较。

//bool g_InvalidInput = false;//全局变量判断输入的合法性
//bool equal(double num1,double num2){
//    if((num1-num2>-0.0000001)&&(num1-num2<0.0000001){
//        return true;
//    }else{
//        return false;
//    }
//}
//
//double power(double base,int expoent){
//    g_InvalidInput = false;
//    //底数为0，指数为负时，输入非法
//    if(equal(base,0)&&exponent<0){
//        g_InvalidInput = true;
//        return 0.0;
//    }
//    unsigned int absExponent = (unsigned int)(expoent);
//    if(expoent<0){
//        absExponent = (unsigned int)(-expoent);
//    }
//    double result = PowerWithUnsignedExpoent(base,absExponent);
//    if(expoent<0){
//        result = 1.0/result;
//    }
//    return result;
//}
//
//double PowerWithUnsignedExpoent(double base,int expoent){
//    if(expoent==0){
//        return 1;
//    }
//    if(expoent==1){
//        return base;
//    }
//    double result = PowerWithUnsignedExpoent(base,expoent>>1);
//    //expoent%2==1
//    if(expoent&&0x1==1){
//        result *= base;
//    }
//    return result;
//}


